﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using Md = Com.Ooohd.Model.Info;
using DBUtility;
namespace Com.Ooohd.Bll.Info
{
    public class ZS_Info
    {
        public void Add(Md.ZS_Info Info)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection)) {
                ct.ZS_Info.InsertOnSubmit(Info);
                ct.SubmitChanges();
            }
        }

        public void Update(Md.ZS_Info Info)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                ct.ZS_Info.Attach(Info,true);
                ct.SubmitChanges();
            }
        }

        public void Delete(int ID)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                var st = ct.ZS_Info.Single(a => a.ID == ID);
                ct.ZS_Info.DeleteOnSubmit(st);
                ct.SubmitChanges();
            }
        }

        public void Delete(string Ids)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                string[] idarray = Ids.Split(',');
                var list = ct.ZS_Info.Where(a => idarray.Contains(a.ID.ToString()));
                ct.ZS_Info.DeleteAllOnSubmit(list);
                ct.SubmitChanges();
            }
        }

        public Md.ZS_Info GetSingle(int ID)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                return ct.ZS_Info.Single(a => a.ID == ID);
            }
        }

        public List<T> Query<T>(string sql, params object[] parameterValues)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                return ct.ExecuteQuery<T>(sql, parameterValues).ToList();
            }
        }

        public List<Md.ZS_Info> Query(string condition, string orderby, int startIndex, int pageSize, ref int pageCount, params object[] param)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.ZS_Info> InfoList = ct.ZS_Info;
                if (!string.IsNullOrEmpty(condition))
                    InfoList = InfoList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    InfoList = InfoList.OrderBy(orderby);
                pageCount = InfoList.Count();
                return InfoList.Skip(startIndex).Take(pageSize).ToList();
            }
        }

        public List<Md.ZS_Info> Query(string condition, string orderby, params object[] param)
        {
            using (var ct = new Md.ZS_InfoDataContext(DbHelperSQL.Connection))
            {
                IQueryable<Md.ZS_Info> InfoList = ct.ZS_Info;
                if (!string.IsNullOrEmpty(condition))
                    InfoList = InfoList.Where(condition, param);
                if (!string.IsNullOrEmpty(orderby))
                    InfoList = InfoList.OrderBy(orderby);
                return InfoList.ToList();
            }
        }

        public Md.ZS_Info CreateModel()
        {
            return new Md.ZS_Info();
        }
    }
}
